// JavaScript Document
var smsCooldownTime = 90;
var newOrderInterval = 5000;//millisec / 1000

var Utils = Object();

/*
 * 气泡式提示信息
 */
var Message = Object();

Message.bottom = 0;
Message.count = 0;
Message.elem = "popMsg";
Message.mvTimer = null;

/* *
 * 开始检查新订单；
 */
Utils.startCheckOrder = function () {
    this.checkOrder();
    window.setInterval("Utils.checkOrder()", newOrderInterval);
}
/*
 * 检查订单
 */
Utils.checkOrder = function () {
    var lastCheckOrder = new Date(document.getCookie('IBG_LastCheckOrder'));
    var today = new Date();
    var today_lastCheckOrder = today - lastCheckOrder;
    if (lastCheckOrder == null || today_lastCheckOrder >= newOrderInterval) {
        document.setCookie('IBG_LastCheckOrder', today.toGMTString());
        try {

            $.getJSON(BASE_URI + 'utils/check_order?jr=' + Math.random(), function (res) {
                if ('undefined' == res || res.error > 0) {
                    return;
                } else {
                    //Message.count ++;
                    //$("#jsDebug").html(res.new_orders_ids + '::' + res.new_paid_ids);
                    //alert(res.new_tbody);
                    if (res.new_orders > 0 || res.new_paid > 0) {
                        //Ucenter.loadPage();
                        $("#spanNewOrder").html(res.new_orders);
                        $("#spanNewPaid").html(res.new_paid);

                        if ('undefined' != res.new_tbody) {
                            $('.order-table thead').after("" + res.new_tbody + "");
                        }
                        var linked = $("#checkOrderLink").attr('href');
                        //高亮颜色
                        $(".order-table tbody").removeClass();
                        var new_orders_ids = res.new_orders_ids;
                        if ('undefined' != new_orders_ids & !Ucenter.isEmpty(new_orders_ids)) {
                            for (var i = 0; i < res.new_orders; i++) {
                                if ($("#orderTB_" + new_orders_ids[i]).length > 0) {
                                    $("#orderTB_" + new_orders_ids[i]).addClass('new_order');
                                }
                            }
                            linked += linked.indexOf('?') > 0 ? '&' : '?';
                            linked += "newIds=" + new_orders_ids;
                        }
                        var new_paid_ids = res.new_paid_ids;
                        if ('undefined' != new_paid_ids & !Ucenter.isEmpty(new_paid_ids)) {
                            for (var i = 0; i < res.new_paid; i++) {
                                if ($("#orderTB_" + new_paid_ids[i]).length > 0) {
                                    $("#orderTB_" + new_paid_ids[i]).addClass('new_paid');
                                }
                            }
                            linked += linked.indexOf('?') > 0 ? '&' : '?';
                            linked += "&payIds=" + res.new_paid_ids;
                        }
                        $("#checkOrderLink").attr('href', linked);
                        Message.show();

                    }
                    if (res.new_msg > 0) {

                    }
                }
            });
            //$("#jsDebug").html($("#jsDebug").html()+'L:Done');
            return false;
        }
        catch (e) {
        }
    }
}


Message.show = function () {
    try {
        //Message.controlSound('msgBeep');
        $("#jquery_jplayer").jPlayer("play");
        //$("#jp_containe .jp-play").trigger('click');
        document.getElementById(Message.elem).style.visibility = "visible"
        document.getElementById(Message.elem).style.display = "block"

        Message.bottom = 0 - parseInt(document.getElementById(Message.elem).offsetHeight);
        Message.mvTimer = window.setInterval("Message.move()", 10);

        document.getElementById(Message.elem).style.bottom = Message.bottom + "px";
    }
    catch (e) {
        alert(e);
    }
}

Message.move = function () {
    try {
        if (Message.bottom == 0) {
            window.clearInterval(Message.mvTimer)
            //Message.mvTimer = window.setInterval("Message.close()", 10000)
        }
        Message.bottom++;
        document.getElementById(Message.elem).style.bottom = Message.bottom + "px";
    } catch (e) {
        alert(e);
    }
}

Message.close = function () {
    document.getElementById(Message.elem).style.visibility = 'hidden';
    document.getElementById(Message.elem).style.display = 'none';
    if (Message.mvTimer) window.clearInterval(Message.mvTimer)
}


document.getCookie = function (sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return decodeURIComponent(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}

document.setCookie = function (sName, sValue, sExpires) {
    var sCookie = sName + "=" + encodeURIComponent(sValue);
    if (sExpires != null) {
        sCookie += "; expires=" + sExpires;
    }

    document.cookie = sCookie;
}

document.removeCookie = function (sName, sValue) {
    document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
